package com.zch.utils.webHook.alarm;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 两个模板： 一个是markdown （推荐）一个是纯文本
 */
public class WebHookMessageTemplate {
    private static final Logger logger = LoggerFactory.getLogger(WebHookMessageTemplate.class);

    public enum MessageType {
        TEXT, MARKDOWN
    }



    /**
     * 创建 Markdown 消息
     * @param title 消息标题
     * @param content Markdown 内容（飞书格式的嵌套 JSON 数组）
     */
    public static String createMarkdownMessage(String title, String content) {
        try {
            return String.format(
                    "{\"msg_type\": \"post\", \"content\": {\"post\": {\"zh-CN\": {\"title\": \"%s\", \"content\": %s}}}}",
                    escapeJson(title), content);
        } catch (Exception e) {
            logger.error("Failed to create markdown message: {}", e.getMessage());
            throw e;
        }
    }
    /**
     * 创建 Markdown 消息
     * @param title 消息标题
     */
    public static String createMarkdownMessage(String title, AbstractAlarmMarkdown abstractAlarmMarkdown) {
        try {
            String alarmTitle = getAlarmTitle(abstractAlarmMarkdown);
            return String.format(
                    "{\"msg_type\": \"post\", \"content\": {\"post\": {\"zh-CN\": {\"title\": \"%s\", \"content\": %s}}}}",
                    escapeJson(alarmTitle), abstractAlarmMarkdown.getMarkdownJson());
        } catch (Exception e) {
            logger.error("Failed to create markdown message: {}", e.getMessage());
        }
        return null;
    }

    /**
     * 创建 Markdown 消息
     */
    public static String createMarkdownMessage( AbstractAlarmMarkdown abstractAlarmMarkdown) {
        try {
            String alarmTitle = getAlarmTitle(abstractAlarmMarkdown);
            return String.format(
                    "{\"msg_type\": \"post\", \"content\": {\"post\": {\"zh-CN\": {\"title\": \"%s\", \"content\": %s}}}}",
                    escapeJson(alarmTitle), abstractAlarmMarkdown.getMarkdownJson());
        } catch (Exception e) {
            logger.error("Failed to create markdown message: {}", e.getMessage());
        }
        return null;
    }

    private static String getAlarmTitle(AbstractAlarmMarkdown abstractAlarmMarkdown) {
        String alarmTitle = abstractAlarmMarkdown.getAlarmTitle();
        if( StringUtils.isNotBlank(abstractAlarmMarkdown.getEnv())) {
            alarmTitle = alarmTitle+"【"+abstractAlarmMarkdown.getEnv()+"】环境";
        }
        return alarmTitle;
    }

    /**
     * 转义 JSON 特殊字符
     */
    private static String escapeJson(String input) {
        return input.replace("\"", "\\\"")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }

    /**
     * 创建文本消息
     */
    public static String createTextMessage(String text) {
        try {
            return String.format("{\"msg_type\": \"text\", \"content\": {\"text\": \"%s\"}}",
                    escapeJson(text));
        } catch (Exception e) {
            logger.error("Failed to create text message: {}", e.getMessage());
            throw e;
        }
    }
}
